package com.linktrip.devicemanager.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.linktrip.devicemanager.entity.DeviceInfo;
import com.linktrip.devicemanager.sms.SMSConfig;
import com.linktrip.devicemanager.websocket.entity.DataStruct;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author CaineZhu E-mail: CaineZhu@me.com
 * @version 创建时间：2017年3月22日 上午11:18:29 
 * @类说明 :短信指令下发
 */

public class CommandUtil {
	
	static Logger logger = LoggerFactory.getLogger(CommandUtil.class);

	public static final int RESPONSE_OK = 200;
	public static final int RESPONSE_ERROR = 400;
	public static final int RESPONSE_AUTHOR = 500;

	public static byte BYTE_REQUEST = 0x01;
	public static byte BYTE_RESPONSE = 0x02;
	public static byte BYTE_UPGRADE_REQUEST = 0x10;
	public static byte BYTE_UPGRADE_RESPONSE = 0x20;
	public static byte BYTE_GET = 0x03;

	public static String getUnlockCommand() {
		return "连旅|UNLOCK|" + ToolKits.getMDHMS() + "|";
	}

	public static DataStruct getUnlockCommand(int lock, int beef, int blink) {
		DataStruct ds = new DataStruct();
		ds.setType(CommandUtil.BYTE_REQUEST);
		ds.setMessageId(CommandUtil.getSequence());
		ds.setSysVersion(0x01);
		ds.setMode(0x00);
		ds.setLockStatus(lock);
		ds.setBlinkStatus((byte) beef);
		ds.setBeepCount((byte) blink);
		ds.setHdwareVer((byte) beef);
		ds.setTimeStamp(System.currentTimeMillis());
		return ds;
	}
	
	public static DataStruct getUnlockCommand(int lock, int beef, int blink,long timestamp) {
		DataStruct ds = new DataStruct();
		ds.setType(CommandUtil.BYTE_REQUEST);
		ds.setMessageId(CommandUtil.getSequence());
		ds.setSysVersion(0x01);
		ds.setMode(0x00);
		ds.setLockStatus(lock);
		ds.setBlinkStatus((byte) beef);
		ds.setBeepCount((byte) blink);
		ds.setHdwareVer((byte) beef);
		ds.setTimeStamp(timestamp);
		return ds;
	}
	
	public static DataStruct getResponseCommand(int lock, int beef, int blink) {
		DataStruct ds = new DataStruct();
		ds.setType(CommandUtil.BYTE_REQUEST);
		ds.setMessageId(CommandUtil.getSequence());
		ds.setSysVersion(0x01);
		ds.setMode(0x00);
		ds.setLockStatus(lock);
		ds.setBlinkStatus((byte) beef);
		ds.setBeepCount((byte) blink);
		ds.setHdwareVer((byte) beef);
		return ds;
	}

	public static void sendCommand(ChannelHandlerContext context, byte[] command) {
		//ByteUtil.loggerBytes(command);
		logger.info("sendCommand===>[to]:{}",ByteUtil.bytes2String(command));
		ByteBuf bf = Unpooled.buffer(command.length);
		bf.writeBytes(command);
		context.writeAndFlush(bf);
		command = null;
	}

	public static byte[] short2Byte(short a) {
		byte[] b = new byte[2];
		b[0] = (byte) (a >> 8);
		b[1] = (byte) (a);

		return b;
	}

	private static short sequenceId = 0;// 序列编号

	/**
	 * 序列 自增
	 */
	public static short getSequence() {
		++sequenceId;
		if (sequenceId > 32766) {
			sequenceId = 0;
		}
		return sequenceId;
	}
	
//	public static String callback(DeviceInfo info){
//		JSONObject obj = (JSONObject) JSONObject.toJSON(info);
//        String res = HttpUtil.httpUrlConnectionForJson(SMSConfig.getCallBackUrl(), "POST", obj.toJSONString(), null);
//        return res;
//	}

}
